﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Web;

namespace Web.live
{
    /// <summary>
    /// ycczk 的摘要说明
    /// </summary>
    public class ycczk : IHttpHandler
    {
        //读取share
        string sharePath = System.Web.HttpContext.Current.Server.MapPath("/live/ck/") + "share.html";
        //读取CK
        string loginPath = System.Web.HttpContext.Current.Server.MapPath("/live/ck/") + "login.html";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string zbj = string.IsNullOrEmpty(context.Request.Form["zbj"]) ? context.Request.QueryString["zbj"] : context.Request.Form["zbj"];
            string pt = string.IsNullOrEmpty(context.Request.Form["pt"]) ? context.Request.QueryString["pt"] : context.Request.Form["pt"];
            string dnid = string.IsNullOrEmpty(context.Request.Form["dnid"]) ? context.Request.QueryString["dnid"] : context.Request.Form["dnid"];
            string ff = string.IsNullOrEmpty(context.Request.Form["ff"]) ? context.Request.QueryString["ff"] : context.Request.Form["ff"];
            string lx = string.IsNullOrEmpty(context.Request.Form["lx"]) ? context.Request.QueryString["lx"] : context.Request.Form["lx"];
            string path = System.Web.HttpContext.Current.Server.MapPath("/live/");
            string filename = path + dnid + ".html";//旧版
            if (lx == "1")//新版
            {
                filename = System.Web.HttpContext.Current.Server.MapPath("/live/" + pt + "/") + dnid + ".html";
            }
            if (ff == "czk")
            {
                StreamWriter sr = null;
                try
                {
                    sr = File.CreateText(filename);
                    sr.WriteLine(context.Request.Form["live"]);
                }
                catch (Exception err)
                {
                    context.Response.Write("44=" + err.Message);
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                        sr.Dispose();
                    }
                }
                context.Response.Write(context.Request.Form["live"]);
            }
            context.Response.End();
        }


        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private string ReadFile(string path)
        {
            string s = "";
            if (File.Exists(path))
            {
                StreamReader myreader = null;
                try
                {
                    myreader = File.OpenText(path);//读取记事本文件
                    s = myreader.ReadToEnd();//从当前位置读取到文件末尾
                }
                catch { s = null; }
                finally
                {
                    if (myreader != null)
                    {
                        myreader.Close();
                        myreader.Dispose();
                    }
                }
            }
            return s;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}